// pages/superpages/purchase/purchase.js
const app = getApp()
Page({
  data: {
    name:'',
    phoneValue:'',
    err:''
  },
  // 输入负责人
  getnameValue(e){
    this.setData({
      name:e.detail.value
    })
  },
  // 输入手机号
  getPhoneValue(e){
    this.setData({
      phoneValue:e.detail
    })
  },
  // 检查手机号
  checkPhone(){
    !(/^1[3456789]\d{9}$/.test(this.data.phoneValue))?this.setData({err:'请输入正确的手机号码'}):this.setData({err:''})
  },
  // 注册商品管理员
  sign(){
    let _this = this
    if(!_this.data.name || !_this.data.phoneValue){
      wx.showToast({
        title: '请完成信息',
        image: '../../../static/icons/err.png',
        duration: 2000
      })
      return
    }
    wx.login({
      success(res){
        let model = {
          account_id: _this.data.phoneValue,
          password: "123",
          status:"2",
          msg: wx.getStorageSync('userInfo'),
          code: res.code
        }
        app.api.sign(model, (res) => {
          if(res.statusCode == 401){
            wx.showToast({
              title: res.data,
              image: '../../../static/icons/err.png',
              duration: 2000
            })
          }else{
            wx.showModal({
              title: '注册成功',
              content: '注册账号是：' + _this.data.phoneValue + ' 默认密码是：123',
              complete(){
                _this.setData({
                  name:'',
                  phoneValue:''
                })
              }
            })
          
          }
        })
      }
    })
  },
  //切换tabbar
  onChange(event) {
    if (event.detail == "0") {
      wx.redirectTo({
        url: '../service/service',
      })
    } else if (event.detail == "2") {
      wx.redirectTo({
        url:'../allservice/allservice'
      })
    }
  }
})